home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / ashc5.arc / UTIL.C < prev   
C/C++ Source or Header  |  1990-07-15  |  6KB  |  329 lines

  1. /*
  2.  *      fatal --- fatal error handler
  3.  */
  4. fatal(str)
  5. char    *str;
  6. {
  7.     printf("%s\n",str);
  8.     exit(-1);
  9. }
  10.  
  11. /*
  12.  *      error --- error in a line
  13.  *                      print line number and error
  14.  */
  15. error(str)
  16. char    *str;
  17. {
  18.     printf("  %s,",Argv[Cfn]);
  19.     printf("%d:  ",Cf_line_num);
  20.     printf("Error -> %s\n",str);
  21.     Err_count++;
  22. }
  23.  
  24.  
  25. /*
  26.  *      warn --- trivial error in a line
  27.  *                      print line number and error
  28.  */
  29. warn(str)
  30. char    *str;
  31. {
  32.     if( ( Pass == 2) && !Lflag ) return;    /* if not listing, don't */
  33.                         /* repeat the warnings   */
  34.     printf("  %s,",Argv[Cfn]);
  35.     printf("%d:  ",Cf_line_num);
  36.     printf("Warning --- %s\n",str);
  37.     Warn_count++;
  38. }
  39.  
  40.  
  41. /*
  42.  *      note --- puts note and cumulative line # in listing
  43.  */
  44. note(str)
  45. char    *str;
  46. {
  47.     if( (Lflag) && (Pass == 2) )
  48.         printf("  NOTE (line %d):  %s\n",Line_num,str);
  49. }
  50.  
  51.  
  52. /*
  53.  *      delim --- check if character is a delimiter
  54.  */
  55. delim(c)
  56. char    c;
  57. {
  58.     if( any(c," \t\n;") )
  59.         return(YES);
  60.     if( c == EOS ) return(YES);
  61.     return(NO);
  62. }
  63.  
  64. /*
  65.  *      skip_white --- move pointer to next non-whitespace char
  66.  */
  67. char *skip_white(ptr)
  68. char    *ptr;
  69. {
  70.     while(*ptr==BLANK || *ptr==TAB || *ptr==NEWLINE)
  71.         ptr++;
  72.     return(ptr);
  73. }
  74.  
  75. /*
  76.  *      eword --- emit a word to code file
  77.  */
  78. eword(wd)
  79. int     wd;
  80. {
  81.     emit(hibyte(wd));
  82.     emit(lobyte(wd));
  83. }
  84.  
  85. /*
  86.  *      emit --- emit a byte to code file
  87.  */
  88. emit(byte)
  89. int byte;
  90. {
  91. #ifdef DEBUG
  92.     printf("%2x @ %4x\n",byte,Pc);
  93. #endif
  94.     Pc++;
  95.     if(Pass==1) return;
  96.     if( Lflag ) {
  97.         if(P_total < P_LIMIT)
  98.             P_bytes[P_total++] = byte;
  99.         }
  100.     E_bytes[E_total++] = byte;
  101.     if(E_total >= E_LIMIT)
  102.         f_record();
  103. }
  104.  
  105. /*
  106.  *      f_record --- flush record out in `S1' format
  107.  */
  108. f_record()
  109. {
  110.     int     stat;
  111.     int     i;
  112.     int     chksum;
  113.  
  114.     if(Pass == 1) {
  115.         E_pc = Pc;
  116.         E_total = 0;
  117.         return;
  118.         }
  119.     if(E_total==0){
  120.         E_pc = Pc;
  121.         return;
  122.         }
  123.     chksum =  E_total+3;    /* total bytes in this record */
  124.     chksum += lobyte(E_pc);
  125.     chksum += E_pc>>8;
  126.     stat = fprintf(Objfil,"S1");   /* record header preamble */
  127.     if( stat != 2 ) fatal("Error writing object file");
  128.     hexout(E_total+3);      /* byte count +3 */
  129.     hexout(E_pc>>8);        /* high byte of PC */
  130.     hexout(E_pc);           /* low byte of PC */
  131.     for(i=0;i<E_total;i++){
  132.         chksum += lobyte(E_bytes[i]);
  133.         hexout(E_bytes[i]);    /* data byte */
  134.         }
  135.     chksum =~ chksum;       /* one's complement */
  136.     hexout(chksum);         /* checksum */
  137.     stat = fprintf(Objfil,"\n");
  138.     if( stat < 0 ) fatal("Error writing object file");
  139.     E_pc = Pc;
  140.     E_total = 0;
  141. }
  142.  
  143. /*
  144.  *      f_S9 --- write the final 'S9' record
  145.  */
  146. f_S9()
  147. {
  148.     int     stat;
  149.     int     chksum;
  150.  
  151.     stat = fprintf(Objfil,"S9");   /* record header preamble */
  152.     if( stat != 2 ) fatal("Error writing object file");
  153.     hexout(3);              /* byte count +1 */
  154.     chksum = 3;
  155.     hexout(Entry_pt>>8);       /* high byte of entry addres */
  156.     chksum += hibyte(Entry_pt);
  157.     hexout(Entry_pt);       /* low byte of entry address */
  158.     chksum += lobyte(Entry_pt);
  159.     chksum =~ chksum;       /* one's complement */
  160.     hexout(chksum); /* checksum */
  161.     stat = fprintf(Objfil,"\n");
  162.     if( stat < 0 ) fatal("Error writing object file");
  163. }
  164.  
  165. char    *hexstr = { "0123456789ABCDEF" } ;
  166.  
  167. hexout(byte)
  168. int     byte;
  169. {
  170.     char hi,lo;
  171.     int stat;
  172.  
  173.     byte = lobyte(byte);
  174.     stat = fprintf(Objfil,"%c%c",hexstr[(byte>>4)&017],hexstr[byte&017]);
  175.     if( stat < 0 ) fatal("Error writing object file");
  176. }
  177.  
  178. /*
  179.  *      print_line --- pretty print input line
  180.  */
  181. print_line()
  182. {
  183.     int     i;
  184.     register char *ptr;
  185.  
  186.         printf ("%04d ",Line_num);
  187.     if(P_total || P_force)
  188.         printf("%04x",Old_pc);
  189.     else
  190.         printf("    ");
  191.  
  192.     for(i=0;i<P_total && i<6;i++)
  193.         printf(" %02x",lobyte(P_bytes[i]));
  194.     for(;i<6;i++)
  195.         printf("   ");
  196.     printf("  ");
  197.  
  198.     if(Cflag && Cycles)
  199.         printf("[%3d ] ",Cycles);
  200.     else
  201.         printf("       ");
  202.     ptr = Line;
  203.     while( *ptr != EOS )   /* just echo the line back out */
  204.         putchar(*ptr++);
  205.     for(;i<P_total;i++){
  206.         if( i%6 == 0 )
  207.             printf("\n         ");
  208.         printf(" %02x",lobyte(P_bytes[i]));
  209.         }
  210.     printf("\n");
  211. }
  212.  
  213. /*
  214.  *      print_cycles --- print # of cycles counted so far
  215.  */
  216. print_cycles()
  217. {
  218.     if( Pass == 2 )
  219.        if( Ctotal )
  220.           printf("  Cycles Counted:  %d\n\n", Ctotal);
  221. }
  222.  
  223.  
  224. /*
  225.  *      any --- does str contain c?
  226.  */
  227. any(c,str)
  228. char    c;
  229. char    *str;
  230. {
  231.     while(*str != EOS)
  232.         if(*str++ == c)
  233.             return(YES);
  234.     return(NO);
  235. }
  236.  
  237. /*
  238.  *      mapdn --- convert A-Z to a-z
  239.  */
  240. char mapdn(c)
  241. char c;
  242. {
  243.     if( c >= 'A' && c <= 'Z')
  244.         return(c+040);
  245.     return(c);
  246. }
  247.  
  248. /*
  249.  *      lobyte --- return low byte of an int
  250.  */
  251. lobyte(i)
  252. int i;
  253. {
  254.     return(i&0xFF);
  255. }
  256. /*
  257.  *      hibyte --- return high byte of an int
  258.  */
  259. hibyte(i)
  260. int i;
  261. {
  262.     return((i>>8)&0xFF);
  263. }
  264.  
  265. /*
  266.  *      head --- is str2 the head of str1?
  267.  */
  268. head(str1,str2)
  269. char *str1,*str2;
  270. {
  271.     while( *str1 != EOS && *str2 != EOS){
  272.         if( *str1 != *str2 )break;
  273.         str1++;
  274.         str2++;
  275.         }
  276.     if(*str1 == *str2)return(YES);
  277.     if(*str2==EOS)
  278.         if( any(*str1," \t\n,+-];*") )return(YES);
  279.     return(NO);
  280. }
  281.  
  282. /*
  283.  *      alpha --- is character a legal letter
  284.  */
  285. alpha(c)
  286. char c;
  287. {
  288.     if( c<= 'z' && c>= 'a' )return(YES);
  289.     if( c<= 'Z' && c>= 'A' )return(YES);
  290.     if( c== '_' )return(YES);
  291.     if( c== '.' )return(YES);
  292.     return(NO);
  293. }
  294. /*
  295.  *      alphan --- is character a legal letter or digit
  296.  */
  297. alphan(c)
  298. char c;
  299. {
  300.     if( alpha(c) )return(YES);
  301.     if( c<= '9' && c>= '0' )return(YES);
  302.     if( c == '$' )return(YES);      /* allow imbedded $ */
  303.     return(NO);
  304. }
  305.  
  306. /*
  307.  *    white --- is character whitespace?
  308.  */
  309. white(c)
  310. char c;
  311. {
  312.     if( c == TAB || c == BLANK || c == '\n' )return(YES);
  313.     return(NO);
  314. }
  315.  
  316. /*
  317.  *    alloc --- allocate memory
  318.  */
  319. char *
  320. alloc(nbytes)
  321. int nbytes;
  322. {
  323.     char *malloc(), *ptr;
  324.  
  325.     ptr = malloc(nbytes);
  326.     if( ptr == (char *)NULL ) fatal("Out of memory");
  327.     return(ptr);
  328. }
  329.